﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Remoting.Messaging;

namespace Artech.ContextPropagation
{
    [Serializable]
    public class ApplicationContext:Dictionary<string,object>
    {
        private const string CallContextKey = "__ApplicationContext";    
        internal const string ContextHeaderLocalName = "__ApplicationContext";
        internal const string ContextHeaderNamespace = "urn:artech.com";

        private void EnsureSerializable(object value)
        {
            if (value == null)
            {
                throw new ArgumentNullException("value");
            }
            if (!value.GetType().IsSerializable)
            {
                throw new ArgumentException(string.Format("The argument of the type \"{0}\" is not serializable!", value.GetType().FullName));
            }
        }       

        public new  object this[string key]
        {
            get
            {
                return base[key];
            }
            set
            {
                this.EnsureSerializable(value);
                base[key] = value;
            }
        }

        #region 定义解析字典数据
        /// <summary>
        /// 用户id
        /// </summary>
        public int UserId
        {
            get
            {
                return (int)this["__UserId"];
            }
            set
            {
                this["__UserId"] = value;
            }
        }

        public string UserCode
        {
            get
            {
                return (string)this["__UserCode"];
            }
            set
            {
                this["__UserCode"] = value;
            }
        }

        public string Ips
        {
            get
            {
                return (string)this["__Ips"];
            }
            set
            {
                this["__Ips"] = value;
            }
        }

        public string Macs
        {
            get
            {
                return (string)this["__Macs"];
            }
            set
            {
                this["__Macs"] = value;
            }
        }

        /// <summary>
        /// 模块Id
        /// </summary>
        public int ModuleId
        {
            get
            {
                return (int)this["__ModuleId"];
            }
            set
            {
                this["__ModuleId"] = value;
            }
        }

        /// <summary>
        /// 用户角色
        /// </summary>
        public int Role
        {
            get
            {
                return (int)this["__Role"];
            }
            set
            {
                this["__Role"] = value;
            }
        }
        #endregion

        public static ApplicationContext Current
        {
            get
            {
                if (CallContext.GetData(CallContextKey) == null)
                { 
                    CallContext.SetData(CallContextKey, new ApplicationContext());
                }

                return CallContext.GetData(CallContextKey) as ApplicationContext;
            }
            set
            {
                CallContext.SetData(CallContextKey, value);
            }
        }
    }
}
